package com.example.android.ouactivity;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.android.ouactivity.adapter.CommentAdapter;
import com.example.android.ouactivity.model.Comment;
import com.example.android.ouactivity.model.LoginBean;
import com.example.android.ouactivity.model.User;
import com.example.android.ouactivity.utils.HttpUtils;
import com.example.android.ouactivity.utils.ReadUserInfo;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.List;

/**
 * 评论列表实现的Activity by chqing
 *进行了网络访问测试 11.17
 * add title and Home 11.17
 */
public class CommentListActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private RecyclerView.LayoutManager layoutManager;
    private CommentAdapter adapter;
    private List<Comment> datas = new ArrayList<>();
    private EditText input;
    private Button submit;
    private TextView tip;
    private Handler handler =new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            String result = msg.obj.toString();
            if(!result.equals("")){
                Gson gson = new Gson();
                datas = gson.fromJson(result,new TypeToken<List<Comment>>(){}.getType());
                tip = (TextView)findViewById(R.id.TV_tip) ;
                tip.setHeight(0);
            }else{
                tip = (TextView)findViewById(R.id.TV_tip) ;
                tip.setText("暂时还没有评论哦！");
            }
            initView();
        }
    };
    private Handler comm_reply = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            String result = msg.obj.toString();
            Gson gson = new Gson();
            LoginBean comm = gson.fromJson(result, LoginBean.class);
            if(comm.getCode()==1){
                Toast.makeText(CommentListActivity.this,comm.getMessage(),Toast.LENGTH_SHORT).show();
                input.setText("");
                String url = "http://www.chqing.cn/comment?news="+mComment.getNews();
                HttpUtils httpUtils = new HttpUtils();
                httpUtils.getNewsJSON(url,handler);
            }
        }
    };
    //用来存储提交的评论的相关信息
    private Comment mComment;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_comment_list);
        //设置返回，及页面标题
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
        if (actionBar != null) {
            actionBar.setTitle("评论");
        }
        //获取上个intent传来的news值，进行相关的配置，获取内容
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        String news = bundle.getString("news");
        mComment = new Comment();
        //要评论的文章对象
        mComment.setNews(Integer.parseInt(news));
        String url = "http://www.chqing.cn/comment?news="+news;
        HttpUtils httpUtils = new HttpUtils();
        httpUtils.getNewsJSON(url,handler);
    }
    public void initView(){
        //初始化组件
        submit = (Button)findViewById(R.id.comm_submit);
        input = (EditText)findViewById(R.id.comm_input);
        recyclerView = (RecyclerView)findViewById(R.id.comm_list_view);
        //创建默认的线性LayoutManager
        layoutManager = new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
        recyclerView.setLayoutManager(layoutManager);
        //添加分割线
        recyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));
        //创建并设置Adapter
        adapter = new CommentAdapter(datas);
        recyclerView.setAdapter(adapter);
        //监听,
        adapter.setOnItemClickListener(new CommentAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, Comment comm) {
                if(input.getText().toString().equals("")){
                    input.setHint("回复"+comm.getAuthor()+":");
                    //设置要回复的评论的id
                    mComment.setComment(String.valueOf(comm.getId()));
                }
            }
        });
        //提交按钮的监听
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ReadUserInfo read = new ReadUserInfo(CommentListActivity.this,"userInfo");
                if(read.hasFile()){
                    String content = input.getText().toString();
                    if(content.equals("")){
                        Toast.makeText(CommentListActivity.this,"请输入评论内容再提交",Toast.LENGTH_SHORT).show();
                    }else{
                        User user = read.getUserByFile();
                        mComment.setContent(content);
                        mComment.setAuthor(String.valueOf(user.getId()));
                        String postString;
                        if(mComment.getComment()==null){
                            postString= "user="+mComment.getAuthor()+"&news="+mComment.getNews()+"&content="+mComment.getContent();
                        }else {
                            postString = "user="+mComment.getAuthor()+"&news="+mComment.getNews()+"&object="+mComment.getComment()+"&content="+mComment.getContent();
                        }
                        HttpUtils http = new HttpUtils();
                        http.getWebJsonByPost("http://www.chqing.cn/comment?sty=rep",comm_reply,postString);
                    }
                }else{
                    Toast.makeText(CommentListActivity.this,"请先登录！",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
    //左上角返回的监听
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case android.R.id.home:
                finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}
